package com.xh.bussiness.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.resource.bean.request.MaterialChapterRequest;
import com.xh.bussiness.resource.dao.MaterialChapterMapper;
import com.xh.bussiness.resource.model.MaterialChapter;
import com.xh.bussiness.resource.service.IMaterialChapterService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialChapterServiceImpl extends BaseServiceImpl<MaterialChapterMapper, MaterialChapter> implements IMaterialChapterService {
    @Override
    public XhPage queryMaterialChapterPage(XhPage page, MaterialChapterRequest bean) {
        QueryWrapper qw=new QueryWrapper<MaterialChapter>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryMaterialChapter(MaterialChapterRequest bean) {
        QueryWrapper qw=new QueryWrapper<MaterialChapter>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public void deleteByChapterId(MaterialChapter bean) {
        QueryWrapper qw=new QueryWrapper<MaterialChapter>();
        qw.eq("material_id",bean.getMaterialId());
        qw.eq("chapter_id",bean.getChapterId());
       this.dao.delete(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(MaterialChapterRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
